package cn.edu.jxust.controller.api.enterprise;

import cn.edu.jxust.common.domain.entity.TradeType;
import cn.edu.jxust.common.domain.dto.TradeTypeVo;
import cn.edu.jxust.common.domain.web.api.enterprise.ITradeTypeController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.TradetypeService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouzhicheng
 * @since 2020-06-29
 */

@Slf4j
@RestController
public class TradeTypeController implements ITradeTypeController {

    @Resource
    private TradetypeService tradetypeService;

    @Override
    public Result getAllTradeType(Integer page, Integer size){

        try{
            IPage<TradeType> pageInfo = tradetypeService.page(new Page<>(page,size));
            if (pageInfo.getRecords() != null && !pageInfo.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pageInfo);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何行业类型信息");
            }
        }catch (Exception e){
            log.error("TradeTypeController --> getAllTradeType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getOneTradeType(Integer id){

        try {
            TradeType byId = tradetypeService.getById(id);
            if(byId != null){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, byId);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何行业类型信息");
            }
        }catch (Exception e){
            log.error("TradeTypeController --> getOneTradeType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result addOneTradeType(TradeTypeVo tradeTypeVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            TradeType tradeType = new TradeType();
            BeanUtil.copyProperties(tradeTypeVo, tradeType);
            return tradetypeService.save(tradeType)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "添加行业类型成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "添加行业类型失败, 请重试!");
        }catch (Exception e){
            log.error("TradeTypeController --> setOneTradeType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result updateOneTradeType(TradeTypeVo tradeTypeVo, BindingResult result){

        if(result.hasErrors() || tradeTypeVo.getId() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try {
            TradeType tradeType = new TradeType();
            BeanUtil.copyProperties(tradeTypeVo, tradeType);
            return tradetypeService.updateById(tradeType)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS,"修改行业类型信息成功！")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED,"修改行业类型信息失败！");
        }catch (Exception e){
            log.error("TradeTypeController --> updateOneTradeType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result deleteOneTradeType(Integer id) {

        try {
            return tradetypeService.removeById(id)
                    ? ResultUtils.resultWithoutData(ResultStatus.DELETED, "删除行业类型成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED,"删除行业类型失败！");
        }catch (Exception e){
            log.error("TradeTypeController --> deleteOneTradeType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

}